package com.njiandan.johnson.vr360library;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.njiandan.johnson.vr360library.constant.VrMode;
import com.njiandan.johnson.vr360library.constant.VrStatus;
import com.njiandan.johnson.vr360library.utils.UIUtils;

/**
 * author：johnsonZhang on 2017/3/29 10:53
 * email：15915356@qq.com
 */
public class VrPlayerActivity extends Activity {

    /**
     * 视屏地址
     */
    public static final String VIDEO_PATH = "videoPath";
    /**
     * 是否是浏览全景图片模式
     */
    public static final String IMAGE_MODE = "imageMode";
    /**
     * 是否是水平平铺播放模式
     */
    public static final String PLANE_MODE = "planeMode";
    /**
     * 是否是窗口播放模式
     */
    public static final String WINDOW_MODE = "windowMode";

    private VrUIController mVrUIController;
    private VrViewWrapper mVrViewWrapper;
    private ImageView mImgBufferAnim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.player_activity_layout);

        init();
    }

    private void init() {
        String videoPath = getIntent().getStringExtra(VIDEO_PATH);
        boolean imageMode = getIntent().getBooleanExtra(IMAGE_MODE, false);
        boolean planeMode = getIntent().getBooleanExtra(PLANE_MODE, false);
        boolean windowMode = getIntent().getBooleanExtra(WINDOW_MODE, false);

        findViewById(R.id.img_full_screen).setVisibility(windowMode ? View.VISIBLE : View.GONE);

        mImgBufferAnim = (ImageView) findViewById(R.id.activity_imgBuffer);
        UIUtils.setBufferVisibility(mImgBufferAnim, !imageMode);
        mVrUIController = new VrUIController(
                (RelativeLayout) findViewById(R.id.player_toolbar_control),
                (RelativeLayout) findViewById(R.id.player_toolbar_progress),
                this, imageMode);

        TextView title = (TextView) findViewById(R.id.video_title);
        title.setText(Uri.parse(videoPath).getLastPathSegment());

        GLSurfaceView glSurfaceView = (GLSurfaceView) findViewById(R.id.surface_view);
        mVrViewWrapper = VrViewWrapper.with(this)
                .setVideoPath(videoPath)
                .setGlSurfaceView(glSurfaceView)
                .setImageMode(imageMode)
                .setPlaneMode(planeMode)
                .init();
        glSurfaceView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mVrUIController.startHideControllerTimer();
                return mVrViewWrapper.handleTouchEvent(event);
            }
        });
        mVrUIController.setAutoHideController(true);
        mVrUIController.setUiCallback(new VrUIController.UICallback() {
            @Override
            public void requestScreenshot() {
                mVrViewWrapper.getTouchHelper().shotScreen();
            }

            @Override
            public void requestFinish() {
                finish();
            }

            @Override
            public void changeDisPlayMode() {
                if (mVrViewWrapper.getStatusHelper().getVrDisPlayMode() == VrMode.DUAL_SCREEN)
                    mVrViewWrapper.getStatusHelper().setVrDisPlayMode(VrMode.SINGLE_SCREEN);
                else mVrViewWrapper.getStatusHelper().setVrDisPlayMode(VrMode.DUAL_SCREEN);
            }

            @Override
            public void changeInteractiveMode() {
                if (mVrViewWrapper.getStatusHelper().getVrInteractiveMode() == VrMode.MOTION)
                    mVrViewWrapper.getStatusHelper().setVrInteractiveMode(VrMode.TOUCH);
                else mVrViewWrapper.getStatusHelper().setVrInteractiveMode(VrMode.MOTION);
            }

            @Override
            public void changePlayingStatus() {
                if (mVrViewWrapper.getStatusHelper().getVrStatus() == VrStatus.PLAYING) {
                    mVrViewWrapper.getMediaPlayer().pauseByUser();
                } else if (mVrViewWrapper.getStatusHelper().getVrStatus() == VrStatus.PAUSED_BY_USER) {
                    mVrViewWrapper.getMediaPlayer().start();
                }
            }

            @Override
            public void playerSeekTo(int pos) {
                mVrViewWrapper.getMediaPlayer().seekTo(pos);
            }

            @Override
            public int getPlayerDuration() {
                return mVrViewWrapper.getMediaPlayer().getDuration();
            }

            @Override
            public int getPlayerCurrentPosition() {
                return mVrViewWrapper.getMediaPlayer().getCurrentPosition();
            }
        });
        mVrViewWrapper.getTouchHelper().setPanoUIController(mVrUIController);

        if (!imageMode) {
            mVrViewWrapper.getMediaPlayer().setPlayerCallback(new VrMediaPlayerWrapper.PlayerCallback() {
                @Override
                public void updateProgress() {
                    mVrUIController.updateProgress();
                }

                @Override
                public void updateInfo() {
                    UIUtils.setBufferVisibility(mImgBufferAnim, false);
                    mVrUIController.startHideControllerTimer();
                    mVrUIController.setInfo();
                }

                @Override
                public void requestFinish() {
                    finish();
                }
            });
        } else mVrUIController.startHideControllerTimer();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mVrViewWrapper.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mVrViewWrapper.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mVrViewWrapper.releaseResources();
    }
}
